/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.daemonna.fluentlee_core.readers;

import com.daemonna.fluentlee_core.pools.server_settings_pool;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.*;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author daemonna
 */
public class security_reader {

    DocumentBuilderFactory domFactory = null;
    DocumentBuilder builder = null;
    XPathFactory factory = null;
    Document doc = null;
    XPath xpath = null;

    /**
     * create xml reader and disable schema/dtd
     */
    public security_reader() {

        domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setNamespaceAware(true); // never forget this!
        domFactory.setValidating(false);

        try {
            domFactory.setFeature("http://xml.org/sax/features/validation", false);
            domFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
            domFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
            domFactory.setFeature("http://xml.org/sax/features/external-general-entities", false);
            domFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
        } catch (Exception e1) {
            Logger.getLogger(settings_reader.class.getName()).log(Level.SEVERE, null, e1);
        }

    }

    /**
     * read settings.xml and load all global_val into data_pool
     */
    public void load_server_security() throws ParserConfigurationException, SAXException, XPathExpressionException, IOException {

        builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        doc = builder.parse("settings/security.xml");
        factory = XPathFactory.newInstance();
        xpath = factory.newXPath();
        XPathExpression expr = xpath.compile("//security/server/*");
        Object result = expr.evaluate(doc, XPathConstants.NODESET);
        NodeList node = (NodeList) result;

        System.out.println("SERVER SECURITY: " + node.getLength());

        for (int i = 0; i < node.getLength(); i++) {

            String type = node.item(i).getAttributes().getNamedItem("type").getTextContent();
            
            if ("jobserver".equals(type)) {
                String allowremotejobs = node.item(i).getTextContent();
                if ("NO".equals(allowremotejobs)) {
                    server_settings_pool.getInstance().allow_remote_jobs = false;
                } else {
                    server_settings_pool.getInstance().allow_remote_jobs = true;
                }
            }
            
            if ("http/doc".equals(type)) {
                String allowremotejobs = node.item(i).getTextContent();
                if ("NO".equals(allowremotejobs)) {
                    server_settings_pool.getInstance().allow_remote_http = false;
                } else {
                    server_settings_pool.getInstance().allow_remote_http = true;
                }
            }
        }
        System.out.println("security END");
    }
}
